<template>
	<!-- <view>
		修改用户信息
	</view> -->
	<view class="box">
		<view class="head" @tap="amend">
			<view>修改用户信息</view>
		</view>
		<view class="content" v-if="info">
			<view class="item">
				<view>头像</view>
				<view class="imag"><image :src="images" mode=""></image></view>
			</view>
			<view class="item">
				<view>用户名</view>
				<view>{{userInfo.username}}</view>
			</view>
			<view class="item">
				<view>昵称</view>
				<view>{{userInfo.nickname}}</view>
			</view>
			<view class="item">
				<view>手机号</view>
				<view>{{userInfo.phone}}</view>
			</view>			
		</view>
		<view class="content" v-else>
			<view class="item">
				<view>头像</view>
				<view class="upload" @tap="Avatar" v-if="infos" >上传头像</view>
				<view class="imag" v-if="!infos"><image :src="images" mode=""></image></view>
			</view>
			<view class="item">
				<view>用户名</view>
				<input type="text" :value="userInfo.username"  placeholder="请输入用户名"/>
			</view>
			<view class="item">
				<view>昵称</view>
				<input type="text" :value="userInfo.nickname" placeholder="请输入昵称" />
			</view>
			<view class="item">
				<view>手机号</view>
				<input type="text" :value="userInfo.phone"  placeholder="请输入手机号"/>
			</view>			
		</view>
		<view class="btn" v-if="info" @tap="logOut">退出登录</view>
		<view class="btn" v-else @tap="uploadss">更新</view>
	</view>
</template>

<script>
	import {getMemberInfo,uploadAvatar} from "../../../api/mine/index.js";
	import {baseUrl} from "../../../api/baseUrl.js"
	import {updateMemberInfo} from "../../../api/mine/login/index.js"
	export default {
		data() {
			return {
				userInfo:[],
				//点击修改用户
				info:true,
				infos:true,
				images:"",
			};
		},
		methods:{
			//修改用户信息
			amend(){
				this.info=false;
				
			},
			//上传头像
			Avatar(){
				var _this=this;
				uni.chooseImage({
				    count: 1, //最多上传几张图片
				    sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: function (res) {
						// console.log(res.tempFilePaths)
						uni.uploadFile({
						    url: 'http://localhost/lejuClient/login/uploadAvatar', //仅为示例，非真实的接口地址
						    filePath:res.tempFilePaths[0],
						    name: 'file',
							timeout:5000,
						    success:(res)=>{
						        console.log(res);
								_this.infos=false;
								  _this.images=JSON.parse(res.data).data.fileUrl;
								console.log(_this.images)
								
							 },
							fail:(res)=>{
								console.log(res);
								},
						    });
				    }
				});
			},
			//点击更新 跳转到登录页面
			uploadss(){
				if(this.images){
					this.userInfo.icon=this.images
				}
				updateMemberInfo(this.userInfo).then(res=>{
					console.log(res)
					if(res.success){
						uni.showToast({
							title:"更新成功,请重新登录获取最新用户信息"
						})
						setTimeout(()=>{
							//首先清空本地存储
							uni.clearStorageSync();
							uni.showToast({
								title:"正在跳转到登录页"
							})
							uni.navigateTo({
								url:"../user/user",	
							})
						},500)
					}else{
						uni.showToast({
							title:"更新失败"
						})
					}
				})
				
			},
			//退出登录
			logOut(){
				uni.removeStorage({
				    key: "token",
				    success: function (res) {
				        console.log('success');
				    }
				});
				// console.log(token)
				// if(token){
				// 	token.remove()
				// }else{
					
				// }
				uni.navigateTo({
					url:"../user/user",
					
				})
			}
		},
		onLoad(){
			//获取用户信息
			getMemberInfo().then(res=>{
				console.log(res)
				this.userInfo=res.data.userInfo
			})
			
		}
	}
</script>

<style lang="scss" scoped>
.box{
	width: 100%;
	height: 100vh;
	background-color:#F1ECE7;
	overflow: hidden;
	.head{
		padding: 0 20px;
		margin-top: 15px;
		display: flex;
		flex-direction:row-reverse;
		view{
			border: 1px solid #353535;
			border-radius: 5px;
			padding: 10px;
		}
	}
	.content{
		width: 100%;
		margin-top: 10px;
		.item{
			padding: 0 20px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 70px;
			border-bottom: 2px solid #e5e5e5;
			line-height: 70px;
			.imag{
				width: 60px;
				height: 60px;
				border-radius: 50%;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.upload{
				border: 1px solid #353535;
				border-radius: 5px;
				padding:5px;
				line-height: 30px;
			}
		}
		input{
			text-align: right;
		}
	}
	.btn{
		width:200px;
		height: 50px;
		background: rgb(53, 78, 68);
		color: rgb(255, 255, 255);
		margin:50px auto 0;
		text-align: center;
		line-height:50px;
	}
}
</style>

